package com.yidu.CashAdministration.controller;


import ch.qos.logback.classic.db.names.TableName;
import com.yidu.CashAdministration.entity.MoneyAllocateEntity;
import com.yidu.CashAdministration.service.MoneyAllocateService;
import com.yidu.Util.util.Dbutil;
import com.yidu.Util.util.TableIdUtil;
import com.yidu.Util.util.TableNameUtil;
import com.yidu.Util.util.TableTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MoneyAllocateController {
    @Autowired
    MoneyAllocateService moneyAllocateService;

    @Autowired
    Dbutil dbutil;

    /**
     * 资金调拨页面
     * @param page 页数
     * @param limit 数量
     * @return
     */
    @RequestMapping("MoneyAllocatePage")
    public Map MapMoneyAllocate(int page, int limit, MoneyAllocateEntity moneyAllocateEntity){
        List<MoneyAllocateEntity>  moneyAllocateEntities = moneyAllocateService.moneyAllocateSelect(page, limit,moneyAllocateEntity);
        int i = moneyAllocateService.selectCount(moneyAllocateEntity);
        Map map = new HashMap();
        map.put("code",0);
        map.put("count",i);
        map.put("msg","");
        map.put("data",moneyAllocateEntities);
        System.out.println(moneyAllocateEntity);
        return map;
    }

    /**
     * 增加数据
     * @param moneyAllocateEntity
     */
    @RequestMapping("MoneyAllocateAdd")
    public void moneyAllocateInsert(MoneyAllocateEntity moneyAllocateEntity,String cashIdk ){
        moneyAllocateEntity.setCashId(cashIdk);
        /*String maxId = dbutil.getMaxId(TableIdUtil.FAID, TableNameUtil.MA, TableTimeUtil.FATime);
        moneyAllocateEntity.setMoneyAllocateId(maxId);*/
        String maxId = dbutil.getMaxId3(TableIdUtil.FAID, TableNameUtil.MA, TableTimeUtil.FATime,moneyAllocateEntity.getBusinessDate());
        moneyAllocateEntity.setMoneyAllocateId(maxId);
        moneyAllocateService.moneyAllocateInsert(moneyAllocateEntity);
        System.out.println(moneyAllocateEntity);
    }




    /**
     *修改
     * @param moneyAllocateEntity
     */
    @RequestMapping("moneyAllocateUpdate")
    public void moneyAllocateUpdate(MoneyAllocateEntity moneyAllocateEntity){
        moneyAllocateService.moneyAllocateUpdate(moneyAllocateEntity);
        System.out.println(moneyAllocateEntity);
    }

    /**
     * 删除数据
     * @param ids
     */
    @RequestMapping("moneyAllocateDelete")
    public Object moneyAllocateDelete(String ids){
        String id[] = ids.split(",");
        MoneyAllocateEntity a = new MoneyAllocateEntity();
        a.setAllocateFlow(1);
        Map map = new HashMap();
        try{
            for (int i = 0; i < id.length; i++) {
                String moneyAllocateId  = id[i];
                moneyAllocateService.moneyAllocateDelete(moneyAllocateId);
                map.put("count", 1);
            }
        }catch (Exception e) {
            e.printStackTrace();
            map.put("count", 2);
        }

        return "";
    }


}
